package com.evil.common.application.enums.permission;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 应用权限类型标识
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ApplicationPermissionSignEnum {

    /**
     * 应用权限类型标识
     */
    SIGN_USER(1, "用户"),
    SIGN_DEPARTMENT(2, "部门"),
    SIGN_ROLE(3, "角色"),
    SIGN_DYNAMIC(4, "动态");

    @Getter
    private final int id;
    @Getter
    private final String name;

    public boolean isDynamic() {
        return SIGN_DYNAMIC.id == this.id;
    }

    public static boolean isDynamic(Integer id) {
        return SIGN_DYNAMIC.id == id;
    }

    public static Optional<ApplicationPermissionSignEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ApplicationPermissionSignEnum isExistById(Integer id) {
        return ApplicationPermissionSignEnum.findById(id).orElseThrow(ApplicationPermissionSignEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "权限类型标识参数错误");
    }
}
